package com.es.poem.controller;

import com.es.poem.config.ResultInfo;
import com.es.poem.entry.People;
import com.es.poem.service.PeopleService;
import com.es.poem.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/people")
public class PeopleController {

    @Autowired
    private PeopleService peopleService;

    @Value("${redis.expireTime}")
    private int expireTime;

    @Resource
    private RedisUtil redisUtil;

    @GetMapping("/findAll")
    public ResultInfo findAllUser(){
        ResultInfo resultInfo = new ResultInfo();
        List<People> peopleList = peopleService.findAll();

        //永久存储
        redisUtil.set("people",peopleList);

        return resultInfo.success(peopleList);
    }


    //获取
    @GetMapping("getRedis")
    public ResultInfo redisget(@RequestParam("key") String key){
        ResultInfo resultInfo = new ResultInfo();
        return resultInfo.success(redisUtil.get(key));
    }

    //指定失效时间
    @GetMapping("expireRedis")
    public ResultInfo expire(@RequestParam("key") String key){
        ResultInfo resultInfo = new ResultInfo();
        return resultInfo.success(redisUtil.expire(key,expireTime));
    }

}
